package com.xiaoshangkou.hellopra.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.mapper.CommentMapper;
import com.xiaoshangkou.hellopra.mapper.MemberMapper;
import com.xiaoshangkou.hellopra.mapper.PostMapper;
import com.xiaoshangkou.hellopra.pojo.Comment;
import com.xiaoshangkou.hellopra.pojo.Member;
import com.xiaoshangkou.hellopra.pojo.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private PostMapper postMapper;

    //分页查询所有帖子
    public Page<Comment> findPage(int page, int size){
        return commentMapper.selectPage(new Page<>(page, size), null);
    }

    public List<Comment> findAll(){
        return commentMapper.findAllComment();

    }

    //根据帖子标题+评论作者查询评论(模糊查询)

    public Page<Comment> findPageByTitleAndAuthor(int page, int size, String titleAuthor){
        String[] split = titleAuthor.split(",");
        String title=split[0];
        String username=split[1];
        //查询pid mid
        String pid = Integer.toString(postMapper.findPidByTitle(title));
        String mid = Integer.toString(memberMapper.findMidByName(username));
        //构建一个查询的wrapper
        QueryWrapper<Comment> wrapper = new QueryWrapper();

        wrapper.like(StringUtils.isNotBlank(pid),"pid",pid);
        wrapper.like(StringUtils.isNotBlank(mid),"mid",mid);
        Page memberPage = commentMapper.selectPage(new Page<>(page, size), wrapper);
        return memberPage;
    }

    public List<Comment> findByTitlAndAuthore(String titleAuthor){

        String[] split = titleAuthor.split(",");
        String title=split[0];
        String username=split[1];
        //查询pid mid
        Integer pid = postMapper.findPidByTitle(title);
        Integer mid = memberMapper.findMidByName(username);
        List<Comment> commet = commentMapper.findByTitlAndAuthore(pid, mid);
        return commet;
    }
}
